package com.htzz.collection.collection;

import com.htzz.collection.bean.DevInfoEntity;
import com.htzz.collection.domain.DevInfoDto;
import com.htzz.collection.domain.ResultVO;
import com.htzz.collection.service.DevInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 *设备相关静态数据接口
 * @author xdz
 * @email ixudezhi@163.com
 * @date 2021-08-31 09:13:01
 */
@Slf4j
@Api(tags = "设备管理接口")
@RestController
@RequestMapping("/devinfo")
public class DevInfoController {
    @Autowired
    private DevInfoService devInfoService;

    /**
     * 列表
     */
    @ApiOperation("列表")
    @PostMapping("/list")
    public ResultVO list(@RequestBody DevInfoDto dto){
        try{

            List<DevInfoEntity> list=devInfoService.seleceListByDto(dto);

            Map<String,Object> map =new HashMap<>();
            map.put("data",list);

            return ResultVO.ok(map);
        } catch (Exception e) {
            log.info(e.getMessage());
            return ResultVO.error(e.getMessage());
        }
    }


    /**
     * 信息
     */
    @ApiIgnore
    @ApiOperation("详情")
    @GetMapping("/info/{id}")
    public ResultVO info(@PathVariable("id") Integer id){
      try{
          Map<String,Object> map =new HashMap<>();
          DevInfoEntity devInfo = devInfoService.getById(id);
          map.put("data",devInfo);
          return ResultVO.ok(map);
      } catch (Exception e) {
          log.info(e.getMessage());
          return ResultVO.error(e.getMessage());
      }
    }

    /**
     * 信息
     */
    @ApiOperation("详情")
    @GetMapping("/detail/{devId}")
    public ResultVO info(@PathVariable("devId") String devId){
        try{
            Map<String,Object> map =new HashMap<>();
            DevInfoEntity devInfo = devInfoService.getByDevId(devId);
            map.put("data",devInfo);
            return ResultVO.ok(map);
        } catch (Exception e) {
            log.info(e.getMessage());
            return ResultVO.error(e.getMessage());
        }
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PutMapping("/save")
    public ResultVO save(@Validated @RequestBody DevInfoEntity devInfo){
        try{
            String info=devInfoService.save(devInfo);
            Map<String,Object> map =new HashMap<>();
            map.put("data",info);
            return ResultVO.ok(map);
        } catch (Exception e) {
            log.info(e.getMessage());
            return ResultVO.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @ApiIgnore
    @ApiOperation("修改")
    @PostMapping("/update")
    public ResultVO update(@Validated @RequestBody DevInfoEntity devInfo){
       try {
            String info=devInfoService.updateById(devInfo);
            Map<String,Object> map =new HashMap<>();
            map.put("data",info);
            return ResultVO.ok(map);
       } catch (Exception e) {
            log.info(e.getMessage());
            return ResultVO.error(e.getMessage());
       }
    }

    /**
     * 删除
     */
    @ApiIgnore
    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public ResultVO delete(@RequestBody String[] devIds){
        try {
            String info =devInfoService.removeByIds(Arrays.asList(devIds));
            Map<String,Object> map =new HashMap<>();
            map.put("data",info);
            return ResultVO.ok(map);
        } catch (Exception e) {
            log.info(e.getMessage());
            return ResultVO.error(e.getMessage());
        }
    }

}
